﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using fanfu.Common;
using fanfu.Model;
using System.Data.SqlClient;

namespace fanfu.Business
{
    public class FriendLinkRule
    {
        //----------后台调用------------

        #region 获取所有友情链接信息
        /// <summary>
        /// 获取所有友情链接信息
        /// </summary>
        public DataTable GetAllFriendLinkInfo()
        {
            string sqlString = "SELECT * FROM FriendLink ORDER BY LinkType ASC, Ord ASC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取指定ID的友情链接
        /// <summary>
        /// 获取指定ID的友情链接
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public FriendLinkModel GetFriendLinkByID(string ID)
        {
            string sqlString = "SELECT TOP 1 * FROM FriendLink WHERE ID=@ID";
            SqlParameter para = new SqlParameter("@ID", ID);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            if (dt.Rows.Count == 1)
                return new FriendLinkModel(dt.Rows[0]);
            else
                return null;
        }
        #endregion

        #region 删除指定ID的友情链接
        /// <summary>
        /// 删除指定ID的友情链接
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteFriendLink(string ID)
        {
            string sqlString = string.Format("UPDATE FriendLink SET Flag={0} WHERE ID={1}", (int)FriendLinkFlag.delete, ID);
            return SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        //----------前台调用------------

        #region 创建所有友情链接HTML
        /// <summary>
        /// 创建所有友情链接HTML
        /// </summary>
        /// <returns></returns>
        public StringBuilder CreateFriendLinkHtml()
        {
            StringBuilder friendLink = new StringBuilder();
           string sqlString = "SELECT * FROM FriendLink WHERE Flag="+(int)FriendLinkFlag.normal+" ORDER BY LinkType ASC, Ord ASC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            foreach (DataRow dr in dt.Rows)
            {
                friendLink.AppendFormat("<li><a title=\"{0}\" target=\"_blank\" href=\"{1}\">{2}</a></li>", dr["LinkSiteName"], dr["LinkUrl"], StringPlus.StringDot(dr["LinkSiteName"].ToString(),28));
                
            }
            return friendLink;
        }
        #endregion
    }
}
